package com.cloud.school.bus.teacherhelper.modules.stuclass;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.Header;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.dialog.CustomWaitDialog.OnKeyCancel;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.BaseErrorListener;
import com.android.support.jhf.network.BaseStateListener;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.assist.FailReason;
import com.android.support.jhf.universalimageloader.core.listener.ImageLoadingListener;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.DateUtils;
import com.android.support.jhf.utils.ImageUtil;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomAlertDialog;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomDatePickerDialog;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomDatePickerDialog.DatePickerListener;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomListDialog;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomListDialog.CustomListDialogItem;
import com.cloud.school.bus.teacherhelper.base.uploadpic.UploadPic;
import com.cloud.school.bus.teacherhelper.base.uploadpic.UploadPic.OnGetPicSucceed;
import com.cloud.school.bus.teacherhelper.base.utils.PictureUtil;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.protocol.accountrelevant.ResetParentsPasswordRequest;
import com.cloud.school.bus.teacherhelper.protocol.accountrelevant.ResetParentsPasswordResponse;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.InClassRequest;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.InClassResponse;
import com.cloud.school.bus.teacherhelper.protocol.classstudent.ModifyAvatarRequest;
import com.cloud.school.bus.teacherhelper.protocol.classstudent.ModifyAvatarResponse;
import com.cloud.school.bus.teacherhelper.protocol.classstudent.ModifyStudentInfoRequest;
import com.cloud.school.bus.teacherhelper.protocol.classstudent.ModifyStudentInfoResponse;

/**
 * 详细资料
 * 
 * @author lenovo
 * 
 */
public class StudentDetailActivity extends BaseActivity {

	private ImageView mHeadImageView;
	private TextView mNickNameTextView;
	private TextView mTrueNameTextView;
	private TextView mGenderTextView;
	private TextView mBirthdayTextView;

	private UploadPic mUploadPic;
	private Student mStudent;
	private int mIndex = 0;
	private int mResultCode = RESULT_CANCELED;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_student_detail);

		init();
	}

	@Override
	protected void init() {

		mStudent = (Student) getIntent().getSerializableExtra("student");
		mIndex = getIntent().getIntExtra("index", 0);

		mUploadPic = new UploadPic(this);

		mHeadImageView = (ImageView) findViewById(R.id.headImageView);
		if (!TextUtils.isEmpty(mStudent.avatar)) {
			ImageLoader.getInstance().displayImage(mStudent.avatar,
					mHeadImageView, mApplication.mCacheDisplayImageOptions,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
							ImageView headImageView = (ImageView) view;
							// headImageView.setImageBitmap(loadedImage);
							if (imageUri.startsWith("http://")) {
								headImageView.setImageBitmap(loadedImage);
							} else if (imageUri.startsWith("file:///")) {
								ImageUtil.setRotaingImageBitmap(
										imageUri.replace("file:///", "/"),
										loadedImage, headImageView);
							} else {
								ImageUtil.setRotaingImageBitmap(imageUri,
										loadedImage, headImageView);
							}
						}
					});
		}
		mNickNameTextView = (TextView) findViewById(R.id.nickNameTextView);
		mNickNameTextView.setText(mStudent.enname);
		mTrueNameTextView = (TextView) findViewById(R.id.nameTextView);
		mTrueNameTextView.setText(mStudent.cnname);
		mGenderTextView = (TextView) findViewById(R.id.genderTextView);
		if ("1".equals(mStudent.sex)) {
			mGenderTextView.setText(getString(R.string.prince));
		} else if ("2".equals(mStudent.sex)) {
			mGenderTextView.setText(getString(R.string.princess));
		}

		mBirthdayTextView = (TextView) findViewById(R.id.birthdayTextView);
		if (!TextUtils.isEmpty(mStudent.birthday)) {
			mBirthdayTextView.setText(mStudent.birthday);
		} else {
			mBirthdayTextView.setText(DateUtils.getCurrentDate("yyyy-MM-dd"));
		}

		TextView parentsAccountTextView = (TextView) findViewById(R.id.parentsAccountTextView);
		parentsAccountTextView.setText(mStudent.username);
		TextView parentsPasswordTextView = (TextView) findViewById(R.id.parentsPasswordTextView);
		parentsPasswordTextView.setText("111111");
		TextView parentPhoneTextView = (TextView) findViewById(R.id.parentPhoneTextView);
		parentPhoneTextView.setText(mStudent.mobile);

		setListener();

		setTitleNavBar();
	}

	@Override
	protected void setListener() {
		Button resetParentsPasswordButton = (Button) findViewById(R.id.resetParentsPasswordButton);
		resetParentsPasswordButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				resetParentsPasswordDialog();
			}
		});

		ViewGroup nameLayout = (ViewGroup) findViewById(R.id.nameLayout);
		nameLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext,
						ModifyNickNameActivity.class);
				intent.putExtra("flag", 1);
				intent.putExtra("hint", mStudent.cnname);
				startActivityForResult(intent, 0);
			}
		});

		ViewGroup modifyLayout = (ViewGroup) findViewById(R.id.modifyLayout);
		modifyLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mUploadPic.doPickPhotoAction(getString(R.string.modify_avator));
			}
		});

		mUploadPic.setOnGetPicSucceed(new OnGetPicSucceed() {

			@Override
			public void onGetPicSucceed(String picPathString, int requestCode) {
				modifyAvatar(picPathString, requestCode);

			}
		});

		ViewGroup nickNameLayout = (ViewGroup) findViewById(R.id.nickNameLayout);
		nickNameLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext,
						ModifyNickNameActivity.class);
				intent.putExtra("flag", 0);
				intent.putExtra("hint", mStudent.enname);
				startActivityForResult(intent, 0);
			}
		});

		ViewGroup genderLayout = (ViewGroup) findViewById(R.id.genderLayout);
		genderLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showGenderDialog();
			}
		});
		ViewGroup birthdayLayout = (ViewGroup) findViewById(R.id.birthdayLayout);
		birthdayLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showBirthdayDialog();
			}
		});
	}

	private void resetParentsPasswordDialog() {
		CustomAlertDialog customAlertDialog = new CustomAlertDialog(mContext,
				R.style.CustomAlertDialog);
		customAlertDialog
				.setTitleMessage(getString(R.string.tips), Color.BLACK);
		customAlertDialog
				.setMessage(getString(R.string.reset_parent_password_to_111111));
		customAlertDialog.setLeftButton(getString(R.string.confirm),
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						resetParentsPassword();
					}
				});
		customAlertDialog.setRightButton(getString(R.string.cancel),
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {

					}
				});
		customAlertDialog.show();
	}

	private void resetParentsPassword() {
		NetworkClient.getNetworkClient().PostRequest(
				new ResetParentsPasswordRequest(mContext, mStudent.parentid),
				new ResetParentsPasswordResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							HandlerToastUI
									.getHandlerToastUI(
											mContext,
											getString(R.string.password_has_been_reset_to_111111));
						} else if ("-2".equals(code)) {
							HandlerToastUI
									.getHandlerToastUI(mContext, "参数不能为空");
						} else if ("-3".equals(code)) {
							HandlerToastUI
									.getHandlerToastUI(mContext, "该家长不存在");
						}
					}
				});
	}

	private void setHeadImageView(String picPathString) {

		ImageLoader.getInstance().displayImage(
				Uri.decode(Uri.fromFile(new File(picPathString)).toString()),
				mHeadImageView, mApplication.mStudentCacheDisplayImageOptions,
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						// mHeadImageView.setImageBitmap(loadedImage);
						if (imageUri.startsWith("http://")) {
							mHeadImageView.setImageBitmap(loadedImage);
						} else if (imageUri.startsWith("file:///")) {
							ImageUtil.setRotaingImageBitmap(
									imageUri.replace("file:///", "/"),
									loadedImage, mHeadImageView);
						} else {
							ImageUtil.setRotaingImageBitmap(imageUri,
									loadedImage, mHeadImageView);
						}
					}
				});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(getString(R.string.detail));
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				cancel(false);
			}
		});
		titleNavBarView.setRightButtonImageString(R.drawable.btn_send_xml,
				getString(R.string.leave), android.R.color.white);
		titleNavBarView.setRightButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// inClass("1");
				inClassDialog("1");
			}
		});
	}

	private void inClassDialog(final String type) {
		CustomAlertDialog customAlertDialog = new CustomAlertDialog(mContext,
				R.style.CustomAlertDialog);
		customAlertDialog
				.setTitleMessage(getString(R.string.tips), Color.BLACK);
		// customAlertDialog.setMessage("是否 " + mStudent.cnname + " 移除班级？");
		customAlertDialog.setMessage(getString(R.string.has_x_left_the_class)
				.replace("%@", " " + mStudent.cnname + " "));
		customAlertDialog.setLeftButton(getString(R.string.yes),
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						inClass(type);
					}
				});
		customAlertDialog.setRightButton(getString(R.string.no),
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {

					}
				});
		customAlertDialog.show();
	}

	private void inClass(final String type) {
		// final String type = "1";
		NetworkClient.getNetworkClient().GetRequest(
				new InClassRequest(mContext, mStudent.studentid,
						mStudent.uid_student, type), new InClassResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							if ("1".equals(type)) {
								// 离班
								HandlerToastUI.getHandlerToastUI(mContext,
										getString(R.string.leave_successful));
								mApplication.mStudentList.remove(mIndex);
								mResultCode = RESULT_OK;
								cancel(true);
							} else if ("0".equals(type)) {
								// 调班
								HandlerToastUI.getHandlerToastUI(mContext,
										"调班成功");
							}
						} else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"studentid 为空");
						} else if ("-5".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"studentuid 为空");
						} else if ("-2".equals(code)) {
							if ("1".equals(type)) {
								// 离班
								HandlerToastUI.getHandlerToastUI(mContext,
										"该学生不在本班级");
							} else if ("0".equals(type)) {
								// 调班
								HandlerToastUI.getHandlerToastUI(mContext,
										"该学生已在本班级");
							}

						}
					}
				});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (null != mUploadPic) {
			mUploadPic.onActivityResult(requestCode, resultCode, data);
		}

		switch (requestCode) {
		case 0: {
			// 修改昵称返回
			switch (resultCode) {
			case RESULT_OK: {
				final String nameString = data.getStringExtra("name");
				final int flag = data.getIntExtra("flag", 0);
				if (0 == flag) {
					modifyStudentInfo("", "", "", mStudent.studentid,
							mStudent.uid_student, nameString, new Runnable() {

								@Override
								public void run() {
									mResultCode = RESULT_OK;
									mStudent.enname = nameString;
									mNickNameTextView.setText(nameString);

								}
							});
				} else if (1 == flag) {
					modifyStudentInfo(nameString, "", "", mStudent.studentid,
							mStudent.uid_student, "", new Runnable() {

								@Override
								public void run() {
									mResultCode = RESULT_OK;
									mStudent.cnname = nameString;
									mTrueNameTextView.setText(nameString);

								}
							});
				}

				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

	private void showBirthdayDialog() {
		CustomDatePickerDialog customDatePickerDialog = new CustomDatePickerDialog(
				mContext, com.android.support.jhf.R.style.DialogFromDownToUp);

		try {
			String[] strings = mBirthdayTextView.getText().toString()
					.split("-");
			int year = Integer.parseInt(strings[0]);
			int monthOfYear = Integer.parseInt(strings[1]) - 1;
			int dayOfMonth = Integer.parseInt(strings[2]);
			customDatePickerDialog.init(year, monthOfYear, dayOfMonth, null);
		} catch (Exception e) {
		}

		customDatePickerDialog.setOkButton(getString(R.string.confirm), 0,
				new DatePickerListener() {

					@Override
					public void onClick(View view, int year, int monthOfYear,
							int dayOfMonth) {
						int tempMonth = monthOfYear + 1;
						int tempDay = dayOfMonth;
						String tempMonthString = tempMonth + "";
						String tempDayString = tempDay + "";
						if (tempMonth < 10) {
							tempMonthString = "0" + tempMonthString;
						}
						if (tempDay < 10) {
							tempDayString = "0" + tempDayString;
						}
						final String tempString = year + "-"
								+ (tempMonthString) + "-" + tempDayString;
						// 当前时间
						final Calendar curCalendar = Calendar.getInstance();
						curCalendar.setTimeInMillis(DateUtils.getDateMillis(
								DateUtils.getCurrentDate("yyyy-MM-dd"),
								"yyyy-MM-dd"));
						// 选择的时间
						final Calendar selCalendar = Calendar.getInstance();
						selCalendar.setTimeInMillis(DateUtils.getDateMillis(
								tempString, "yyyy-MM-dd"));
						// 选择的时间一定要小于当前时间
						if (selCalendar.compareTo(curCalendar) <= 0) {
							modifyStudentInfo("", "", tempString,
									mStudent.studentid, mStudent.uid_student,
									"", new Runnable() {

										@Override
										public void run() {
											mResultCode = RESULT_OK;
											mStudent.birthday = tempString;
											int age = 0;
											try {
												age = curCalendar.get(Calendar.YEAR)-selCalendar.get(Calendar.YEAR)-1;
												if(age<0){
													age = 0;
												}
											} catch (Exception e) {
											}
											mStudent.age = age + "";
											mBirthdayTextView
													.setText(tempString);

										}
									});
						} else {
							HandlerToastUI
									.getHandlerToastUI(
											mContext,
											getString(R.string.please_set_the_correct_birthday));
						}

					}
				});
		customDatePickerDialog.setCancelButton(getString(R.string.cancel), 0,
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {

					}
				});
		customDatePickerDialog.show();
	}

	private void showGenderDialog() {
		CustomListDialog customListDialog = CustomListDialog
				.getCustomListDialog(mContext,
						com.android.support.jhf.R.style.DialogFromDownToUp);
		List<CustomListDialogItem> customListDialogItems = new ArrayList<CustomListDialog.CustomListDialogItem>();
		CustomListDialogItem customListDialogItem1 = new CustomListDialogItem();
		customListDialogItem1.text = getString(R.string.prince);
		customListDialogItem1.textColor = getResources().getColor(
				R.color.btn_bg_blue);
		customListDialogItem1.textSize = 18;
		customListDialogItems.add(customListDialogItem1);
		CustomListDialogItem customListDialogItem2 = new CustomListDialogItem();
		customListDialogItem2.text = getString(R.string.princess);
		customListDialogItem2.textColor = getResources().getColor(
				R.color.btn_bg_blue);
		customListDialogItem2.textSize = 18;
		customListDialogItems.add(customListDialogItem2);
		customListDialog.setMessage(customListDialogItems);
		customListDialog.setTitle(getString(R.string.gender));
		customListDialog.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int arg2, long arg3) {
				if (0 == arg2) {
					// 男孩
					modifyStudentInfo("", (arg2 + 1) + "", "",
							mStudent.studentid, mStudent.uid_student, "",
							new Runnable() {

								@Override
								public void run() {
									mResultCode = RESULT_OK;
									mStudent.sex = (arg2 + 1) + "";
									mGenderTextView
											.setText(getString(R.string.prince));

								}
							});

				} else if (1 == arg2) {
					// 女孩
					modifyStudentInfo("", (arg2 + 1) + "", "",
							mStudent.studentid, mStudent.uid_student, "",
							new Runnable() {

								@Override
								public void run() {
									mResultCode = RESULT_OK;
									mStudent.sex = (arg2 + 1) + "";
									mGenderTextView
											.setText(getString(R.string.princess));

								}
							});

				}
			}
		});
		customListDialog.show();
	}

	private void modifyAvatar(final String picPathString, final int requestCode) {
		// "studentid": "49431"
//		showWaitDialog("正在上传请稍后...", new OnKeyCancel() {
//
//			@Override
//			public void onKeyCancelListener() {
//				NetworkClient.getNetworkClient().cancelRequests(mContext, true);
//			}
//		});
		String datePicString = PictureUtil.getPicString(picPathString, 512);
		if (null == datePicString) {
			return;
		}
		String fext = picPathString
				.substring(picPathString.lastIndexOf(".") + 1);
		String fsize = datePicString.length() + "";
		NetworkClient.getNetworkClient().PostRequest(
				new ModifyAvatarRequest(mContext, datePicString, fext, fsize,
						mStudent.studentid), new ModifyAvatarResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {

						if (UploadPic.PHOTO_PICKED_WITH_DATA == requestCode) {
							mUploadPic.removePicture(picPathString);
						}

						if ("1".equals(code)) {
							HandlerPostUI.getHandlerPostUI(new Runnable() {

								@Override
								public void run() {
									// setHeadImageView(picPathString);
									ImageLoader
											.getInstance()
											.displayImage(
													avatarUrl,
													mHeadImageView,
													mApplication.mStudentCacheDisplayImageOptions,
													new SimpleImageLoadingListener() {
														@Override
														public void onLoadingComplete(
																String imageUri,
																View view,
																Bitmap loadedImage) {
															// mHeadImageView.setImageBitmap(loadedImage);
															if (imageUri
																	.startsWith("http://")) {
																mHeadImageView
																		.setImageBitmap(loadedImage);
															} else if (imageUri
																	.startsWith("file:///")) {
																ImageUtil
																		.setRotaingImageBitmap(
																				imageUri.replace(
																						"file:///",
																						"/"),
																				loadedImage,
																				mHeadImageView);
															} else {
																ImageUtil
																		.setRotaingImageBitmap(
																				imageUri,
																				loadedImage,
																				mHeadImageView);
															}
														}
													});
								}
							});

							HandlerToastUI.getHandlerToastUI(mContext,
							/* "修改学生头像成功" */getString(R.string.successful));
							DebugLog.logI("avatarUrl : " + avatarUrl);
							mStudent.avatar = avatarUrl;
							mResultCode = RESULT_OK;
						} else if ("-36".equals(code)) {
							HandlerToastUI
									.getHandlerToastUI(mContext, "学生id为空");
						} else if ("-40".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext, "文件流为空");
						} else if ("-42".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext, "文件已存在");
						}
						// if (TextUtils.isEmpty(responseString)) {
						// HandlerToastUI.getHandlerToastUI(mParentContext,
						// "头像上传失败");
						// } else {
						// DebugLog.logI("responseString : " + responseString);
						// }
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								hideWaitDialog();
							}
						});
					}
				}, null, new BaseErrorListener() {
					@Override
					public void onFailure(int statusCode, Header[] headers,
							byte[] responseBody, Throwable error) {
						if (UploadPic.PHOTO_PICKED_WITH_DATA == requestCode) {
							mUploadPic.removePicture(picPathString);
						}
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								hideWaitDialog();
							}
						});
					}
				});
	}

	private void modifyStudentInfo(String cnname, String sex, String birthday,
			String studentid, String uid, String enname, final Runnable r) {
		NetworkClient.getNetworkClient().PostRequest(
				new ModifyStudentInfoRequest(mContext, cnname, sex, birthday,
						studentid, uid, enname),
				new ModifyStudentInfoResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
							/* "修改学生信息成功" */getString(R.string.successful));
							HandlerPostUI.getHandlerPostUI(r);
						} else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"studentid为空");
						} else if ("-1015".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"班级中没有改学生");
						} else if ("-2".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"性别或生日为空");
						}
					}
				});
	}

	/**
	 * 
	 * @param delete
	 */
	private void cancel(boolean delete) {
		if (RESULT_OK == mResultCode) {
			Intent intent = new Intent();
			if (!delete) {
				intent.putExtra("index", mIndex);
				intent.putExtra("student", mStudent);
			}
			setResult(mResultCode, intent);
		}
		finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			cancel(false);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
